package org.autobot.worker;

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.InputEvent;

import org.apache.log4j.Logger;
import org.autobot.store.ClickList;

/**
 * <p>
 * <b>Overview:</b>
 * <p>
 * 
 * 
 * <pre>
 * Creation date: Feb 16, 2015
 * @author Amit Kshirsagar
 * @version 1.0
 * @since
 * 
 * <p><b>Modification History:</b><p>
 * 
 * 
 * </pre>
 */
public class AutoClickerBot extends Robot implements Runnable {

	/**
	 * Initiate the log4j Logger for debugging purposes.
	 * 
	 */
	private static Logger log4j = Logger.getLogger(AutoClickerBot.class);

	/**
	 * Print object values by default. Add required attributes of the object.
	 */

	@Override
	public String toString() {
		return "AutoClickerBot: ";
	}

	public AutoClickerBot(ClickList clickList) throws AWTException {
		super();
		this.clickList = clickList;
	}

	public static void main(String args[]) {
		Robot bot = null;
		try {
			bot = new Robot();
		} catch (Exception failed) {
			log4j.error("Failed instantiating Robot: " + failed, failed);
		}

	}

	private boolean continueClicks = false;
	private boolean terminiateBot = false;

	private int mask = InputEvent.BUTTON1_DOWN_MASK;

	private ClickList clickList = null;

	public boolean isTerminiateBot() {
		return terminiateBot;
	}

	public void setTerminiateBot(boolean terminiateBot) {
		this.terminiateBot = terminiateBot;
	}

	public boolean isContinueClicks() {
		return continueClicks;
	}

	public void setContinueClicks(boolean continueClicks) {
		this.continueClicks = continueClicks;
	}

	public ClickList getClickList() {
		return clickList;
	}

	public void setClickList(ClickList clickList) {
		this.clickList = clickList;
	}

	private int clickCount = 0;

	@Override
	public void run() {

		do {
			if (isContinueClicks()) {
				log4j.debug("Clicking Point: " + clickList.get(clickCount));
				this.mouseMove(clickList.get(clickCount).getHorizontal(), clickList.get(clickCount).getVertical());
				this.mousePress(mask);
				this.mouseRelease(mask);
			}
			clickCount++;
		} while (!isTerminiateBot());

	}
}
